package user_server

import (
	"context"
	"strings"
	"time"

	"google.golang.org/grpc"
	common "jihe.common"
	"jihe.common/protos/user_server/proto"
)

var once = false
var conns []proto.UserClient

func GetClient() []proto.UserClient {
	if !once {
		for i := 0; i < len(common.Configer.Grpc.UserServers); i++ {
			grpcConn, err := grpc.Dial(common.Configer.Grpc.UserServers[i], grpc.WithInsecure())
			if err != nil {
				return conns
			}
			conns = append(conns, proto.NewUserClient(grpcConn))
		}
		once = true
	}
	return conns
}

func UserRegiste(c context.Context, in *proto.UserDUser) (out *proto.UserDUser, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserRegiste(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserUpdateInfo(c context.Context, in *proto.UserDUser) (out *proto.UserDUser, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserUpdateInfo(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserUpdatePhone(c context.Context, in *proto.UserDUser) (out *proto.UserDUser, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserUpdatePhone(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserGetByUid(c context.Context, in *proto.UserDUser) (out *proto.UserDUser, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserGetByUid(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserSubmitAudit(c context.Context, in *proto.UserDUserAudit) (out *proto.UserNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserSubmitAudit(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserGetAuditByUids(c context.Context, in *proto.UserGetAuditByUidsReq) (out *proto.UserDUserAudits, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserGetAuditByUids(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserAuditStatus(c context.Context, in *proto.UserDUserAudit) (out *proto.UserNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserAuditStatus(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserUpdateStatus(c context.Context, in *proto.UserDUser) (out *proto.UserNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserUpdateStatus(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserDUserList(c context.Context, in *proto.UserListReq) (out *proto.UserListRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserDUserList(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserCUserCreate(c context.Context, in *proto.UserCUser) (out *proto.UserCUser, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserCUserCreate(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserCUserUpdate(c context.Context, in *proto.UserCUser) (out *proto.UserCUser, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserCUserUpdate(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserCreateCDUser(c context.Context, in *proto.UserCDUser) (out *proto.UserNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserCreateCDUser(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserGetCUsersByIds(c context.Context, in *proto.UserGetCUsersByIdsReq) (out *proto.UserCUsers, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserGetCUsersByIds(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserGetCDUsersByDUid(c context.Context, in *proto.UserCDUser) (out *proto.UserGetCDUsersByDUidRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserGetCDUsersByDUid(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserGetUserByIds(c context.Context, in *proto.UserGetUserByIdsReq) (out *proto.UserUsers, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserGetUserByIds(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserUpdateUserById(c context.Context, in *proto.UserUser) (out *proto.UserNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserUpdateUserById(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserGetByOpenid(c context.Context, in *proto.UserDUser) (out *proto.UserDUser, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserGetByOpenid(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserDUserListByIds(c context.Context, in *proto.UserDUserListByIdsReq) (out *proto.UserDUsers, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserDUserListByIds(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserAddAmount(c context.Context, in *proto.UserAccount) (out *proto.UserNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserAddAmount(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UserGetAmount(c context.Context, in *proto.UserAccount) (out *proto.UserAccount, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserGetAmount(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UpdateUserPhotos(c context.Context, in *proto.UserDUser) (out *proto.UserNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UpdateUserPhotos(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UpdateUserSex(c context.Context, in *proto.UserDUser) (out *proto.UserNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UpdateUserSex(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func UpdateUserBodyData(c context.Context, in *proto.UserDUser) (out *proto.UserNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UpdateUserBodyData(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func GetUserCList(c context.Context, in *proto.UserListReq) (out *proto.UserCListRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].UserCUserList(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}
